/*
角色信息界面
*/
window._NPCDL = {

    paramLength:65,//五位图边长

    _MAINLABEL:null,
    _PAGEINDEX:0,
    _ROLEARR:null,
    _CURRENTSENCE:null,//当前场景
    _PARENTNODE:null,
    _UISCAL:1,

    _NPCPLACE:{},

    initUi:function(parentNode,param){

        if(!param)
            return;

        _NPCDL._CURRENTSENCE = param.sence;
        _NPCDL._PARENTNODE = parentNode;

        var uiObj = _GRM.getNodeObj('npcInfoLabel');
        var mainLabel = uiObj.node;
        _NPCDL._MAINLABEL = mainLabel;
        _NPCDL._PAGEINDEX=param.roleIndex?param.roleIndex:0;
        _NPCDL._ROLEARR=param.roleArr;

        if(uiObj.isnew){
            this.initButton(mainLabel);
        }

        _NPCDL.refreshPage();
        parentNode.addChild(mainLabel);

        return mainLabel;
    },

    initButton:function(mainLabel){

        var btnLeft = mainLabel.getChildByName('page').getChildByName('btnLeft');
        var btnRight = mainLabel.getChildByName('page').getChildByName('btnRight');
        btnLeft.pageDir=-1;
        btnRight.pageDir=1;
        btnLeft.on(cc.Node.EventType.TOUCH_START, _NPCDL.pageClick, btnLeft);
        btnRight.on(cc.Node.EventType.TOUCH_START, _NPCDL.pageClick, btnRight);

        mainLabel.getChildByName('mask')
            .on(cc.Node.EventType.TOUCH_START, function(){
                    _GRM.putNode(this);
            }, mainLabel);

        var skillNodes = mainLabel.getChildByName('mainLabel')
        .getChildByName('skill').getChildren();
        for(var i=0;i<skillNodes.length;i++){
            var skillNode = skillNodes[i];
            skillNode.on(cc.Node.EventType.TOUCH_START, function(){
                
                _MGD.showGoodsUi(null,_IOF.createObjByName(this.skillname,null),{
                    sence:_MAP,
                    pos:_MGD._POSTUJ,
                    skillIndex:0,
                    scal:1
                });

            }, skillNode);
        }

        var placeNode = mainLabel.getChildByName('mainLabel').getChildByName('place');
        placeNode.on(cc.Node.EventType.TOUCH_START, function(){
            console.log(this.mapName);
            if(_GAMEDATAHELPER.isChapterOpen(this.mapName)){

                _NPCDL.closeUi();

                _MAP.jumpToMap(this.mapName,'下');
            }else{
                //_TIPS.showMessage('此地图尚未解锁无法传送');
                _CHOICEMANAGER.showChoice(_MAP.node,'ygbh',{map:this.mapName});
            }
        }, placeNode);

    },

    closeUi:function(){
        _GRM.putNode(_NPCDL._MAINLABEL);

        if(_MANAGERTJ._MAINLABEL)
            _GRM.putNode(_MANAGERTJ._MAINLABEL);
    },

    pageClick:function(){
        //console.log('翻页',this.pageDir);
        if(_NPCDL._PAGEINDEX+this.pageDir<0
            ||_NPCDL._PAGEINDEX+this.pageDir>_NPCDL._ROLEARR.length-1){
            return;
        }else{
            _NPCDL._PAGEINDEX+=this.pageDir;
            _NPCDL.refreshPage();
        }
    },

    refreshPage:function(){

        var role = _NPCDL._ROLEARR[_NPCDL._PAGEINDEX].value;
        var roleconfig = _NPC[role];
        var roleplaces = _NPCDL.getNpcPlace(role);
        var roleplace = null;
        //console.log(role);
        var mainLabel = _NPCDL._MAINLABEL;
        var faceNode = mainLabel.getChildByName('mainLabel').getChildByName('face');
        var infoNode = mainLabel.getChildByName('mainLabel').getChildByName('info');
        var placeNode = mainLabel.getChildByName('mainLabel').getChildByName('place');

        for(var i=0;i<roleplaces.length;i++){
            if(_GAMEDATAHELPER.isChapterOpen(roleplaces[i])){
                roleplace=roleplaces[i];
            }
        }

        if(roleplace){
            placeNode.getComponent(cc.Label).string = "出现地点:"+roleplace;
            placeNode.mapName = roleplace;
        }else{
            placeNode.getComponent(cc.Label).string = "出现地点:???";
            placeNode.mapName = roleplaces.length>0?roleplaces[0]:null;
        }
        

        //刷新形象
        _CU.initAllAniNode(roleconfig.pf, mainLabel.getChildByName('mainLabel').getChildByName('player'));

        mainLabel.getChildByName('page').getChildByName('label').getComponent(cc.Label).string = role;

        //刷新头像
        _NPCDL.refreshFace(faceNode,roleconfig);

        //刷新修为
        _NPCDL.refreshXw(infoNode,roleconfig);

        //刷新技能
        _NPCDL.refreshAllSkill();

        //绘制五位图
        _NPCDL.drawSix(mainLabel
            .getChildByName('mainLabel')
            .getChildByName('five')
            .getChildByName('attr'),
            mainLabel
            .getChildByName('mainLabel')
            .getChildByName('five')
            .getChildByName('data').getComponent(cc.Graphics),[roleconfig.plv,roleconfig.dlv,roleconfig.ilv,roleconfig.mlv,roleconfig.hlv]);

    },


    refreshXw:function(infoNode,roleconfig){

        var xwNodeArr = infoNode.getChildren();
        for(var i=0;i<xwNodeArr.length;i++){
            xwNodeArr[i].active = false;
        }
        var xwIndex = 0;
        for(var key in roleconfig.npcxw){

            xwNodeArr[xwIndex].active = true;
            xwNodeArr[xwIndex].getComponent(cc.Label).string = _XW[key].label+":"+roleconfig.npcxw[key];
            xwNodeArr[xwIndex].color = new cc.color(_CS.attrColor[_XW[key].attr]);
            xwIndex++;
            
        }

    },

    refreshFace:function(faceNode,roleconfig){

        faceNode.getComponent(cc.Sprite).spriteFrame = _GRM.getFaceFrame(roleconfig.face);
        //faceNode.getChildByName('level').getComponent(cc.Label).string = "LV."+roleData.lv;
        //刷新种族 五行  职业
        var roleQua = roleconfig.level;
        var rolerace = roleconfig.race;
        var rolejob = roleconfig.job;
        var ftypeColor = window._CS.ftype[roleconfig.ftype]?window._CS.ftype[roleconfig.ftype].color:"#FFFFFF";
        var quaColor = window._CS.qua[roleQua]?window._CS.qua[roleQua].color:"#FFFFFF";
        faceNode.getChildByName('level').getChildByName('label').getComponent(cc.Label).string = roleQua;
        faceNode.getChildByName('level').color = new cc.color(quaColor);
        faceNode.getChildByName('ftype').getChildByName('ftypeStr').getComponent(cc.Label).string = roleconfig.ftype;
        faceNode.getChildByName('ftype').color = new cc.color(ftypeColor);
        faceNode.getChildByName('job').getChildByName('label').getComponent(cc.Label).string = window._JOB[rolejob]?window._JOB[rolejob].sname:'?';
        faceNode.getChildByName('job').color = new cc.color(window._JOB[rolejob].color);
        faceNode.getChildByName('zhong').getChildByName('label').getComponent(cc.Label).string = window._RACE[rolerace].name;
        faceNode.getChildByName('zhong').color = new cc.color(window._RACE[rolerace].color);

    },

    
    //画六维图，并填充
	drawSix(attrNodes,graphics,dataArr) {

        attrNodes.getChildByName('plv').getComponent(cc.Label).string='武:'+dataArr[0];
        attrNodes.getChildByName('plv').color = new cc.color(_CS.attrColor['attack'])
        attrNodes.getChildByName('dlv').getComponent(cc.Label).string='统:'+dataArr[1];
        attrNodes.getChildByName('dlv').color = new cc.color(_CS.attrColor['defend'])
        attrNodes.getChildByName('ilv').getComponent(cc.Label).string='智:'+dataArr[2];
        attrNodes.getChildByName('ilv').color = new cc.color(_CS.attrColor['mattack'])
        attrNodes.getChildByName('mlv').getComponent(cc.Label).string='德:'+dataArr[3];
        attrNodes.getChildByName('mlv').color = new cc.color(_CS.attrColor['mdefend'])
        attrNodes.getChildByName('hlv').getComponent(cc.Label).string='体:'+dataArr[4];
        attrNodes.getChildByName('hlv').color = new cc.color(_CS.attrColor['maxHp'])

        var per = _NPCDL.paramLength/_MRDL.maxlength;
        _CU.drawData(graphics,dataArr,per);
	},


    getCurrentRole:function(){
        return _NPCDL._ROLEARR[_NPCDL._PAGEINDEX];
    },

     //刷新种族和称号
     refreshRaceAndTitle:function(role,bossLvNode){

        var rolerace = role.race?role.race:"wu";
        var roletitle = role.roletitle?role.roletitle:"无";

        bossLvNode.getChildByName('label').getComponent(cc.Label).string = _RACE[rolerace].name;
        bossLvNode.color = new cc.color(_RACE[rolerace].color);
        bossLvNode.getChildByName('job').getComponent(cc.Label).string = roletitle;
        
    },

    getRaceOfRole:function(role){

        if(_NPC[role.npctype]){
            return _NPC[role.npctype].race;
        }

        return "wu";

    },

    refreshAllSkill:function(){
        var role = _NPCDL._ROLEARR[_NPCDL._PAGEINDEX].value;
        var roleconfig = _NPC[role];
        var mainLabel = _NPCDL._MAINLABEL;
        var skillNodes = mainLabel.getChildByName('mainLabel')
                        .getChildByName('skill').getChildren();
        for(var i=0;i<skillNodes.length;i++){
            var skillNode = skillNodes[i];
            skillNode.active = false;
        }

        if(roleconfig&&roleconfig.skill){
            for(var i=0;i<roleconfig.skill.length;i++){
                if(skillNodes[i]){
                    skillNodes[i].skillname = roleconfig.skill[i];
                    _NPCDL.refreshSkill(skillNodes[i],roleconfig.skill[i]);
                }
            }
        }
    },

    refreshSkill:function(skillNode,skill){

        skillNode.active = true;

        var bgNode = skillNode.getChildByName('guan_skill');
        var nameNode = skillNode.getChildByName('label');
        var typeNode = skillNode.getChildByName('qiu');
        var noneNode = skillNode.getChildByName('img_add');
        var starNode = skillNode.getChildByName('xinxin');
        var ftypeNode = skillNode.getChildByName('wuxin'); 
        var skillObj = null;

        if(skill){

            noneNode.active = false;
            starNode.active = true;
            //typeNode.active = true;
            nameNode.active = true;
            bgNode.active = true;
            ftypeNode.active = true;

            nameNode.getComponent(cc.Label).string = skill;
            if(_SKILL[skill]){
                typeNode.getChildByName('label').getComponent(cc.Label).string = '主';
                skillObj = _SKILL[skill]
            }else{
                typeNode.getChildByName('label').getComponent(cc.Label).string = '被';
                skillObj = _SKILLBD[skill];
            }

            //if(skillObj&&skillObj.bossflag){
            if(_SKILLHEPLER.isZhuanshu(skill)){
                starNode.active = true;
            }else{
                starNode.active = false;
            }

            if(skillObj&&skillObj.ftype&&_CS.ftype[skillObj.ftype]){
                ftypeNode.active =true;
                ftypeNode.getChildByName('label').getComponent(cc.Label).string = skillObj.ftype;
                ftypeNode.color = new cc.color(_CS.ftype[skillObj.ftype].color);
            }else{
                ftypeNode.active =false;
            }

            if(
                skillObj
                &&skillObj.qua
                &&_CS.qua[skillObj.qua]
                &&_CS.qua[skillObj.qua].color
            ){
                bgNode.color = new cc.color(_CS.qua[skillObj.qua].color);
            }else{
                bgNode.color = new cc.color("#000000");
            }

        }else{

            noneNode.active = true;
            starNode.active = false;
            typeNode.active = false;
            nameNode.active = false;
            bgNode.active = false;
            ftypeNode.active = false;

        }

    },

    getNpcPlace:function(npcname){
        if(!_NPCDL._NPCPLACE[npcname]){

            _NPCDL._NPCPLACE[npcname]=[];

            for(var key in _NPCMAP){
                for(var i=0;i<_NPCMAP[key].npc.length;i++){

                    if(_NPCMAP[key].npc[i].npc==npcname){
                        _NPCDL._NPCPLACE[npcname].push(key);
                    }

                }
            }
        }

        return _NPCDL._NPCPLACE[npcname];
    }

}